// pages/yanzheng/yanzheng.js
var util = require("../../utils/util.js")
let api = require('../../api/api.js')
var codeTimer = null;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    codeHidden: true,
    timerNumber: 60
  },
  phoneCall: function(e) { // 底部打电话
    wx.makePhoneCall({
      phoneNumber: "18511175656"
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  code: function(e) {
    this.setData({
      code: e.detail.value
    })
  },

  getCode: function(data) {
    var that = this
    var phone = this.data.phone
    var data = {}
    data.mobile = phone
    api.verify(data, res => {
      console.log(res)
      if (res.data.code != "成功") {
        wx.showModal({
          title: '提示',
          content: res.data.code,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        that.setData({
          codeHidden: !that.data.codeHidden,
          timerNumber: 60,
          indexData: res.data.result
        });
        codeTimer = setInterval(that.codeTimerFn, 1000);
      }
    })
  },
  codeTimerFn: function() {
    var that = this;
    that.setData({
      timerNumber: that.data.timerNumber - 1
    });
    if (that.data.timerNumber == 0) {
      clearInterval(codeTimer);
      codeTimer = null;
      that.setData({
        codeHidden: !that.data.codeHidden
      });
    }
  },
  login: function(e) {
    var that = this
    var phone = that.data.phone
    var code = that.data.code
    var data = {}
    data.mobile = phone
    data.verify = code

    api.page(data, res => {
      console.log(res)
      if (res.data.code != "成功") {
        wx.showModal({
          title: '提示',
          content: res.data.code,
          showCancel: false,
          success(res) {}
        })
      } else {
        app.globalData.userInerInfo = res.data.result
        wx.setStorage({
          key: 'mobile',
          data: phone
        })
        wx.setStorage({
          key: 'verify',
          data: code
        })
        if (res.data.result.level == 0) {
          util.navigateTo("/pages/A/index/index")
        } else {
          util.navigateTo("/pages/B/index/index")
        }
      }
    })


    
    // console.log(code)
    // util.sendRequest("/api/Login/judgment", {
    //   mobile: phone,
    //   verify: code
    // }, "POST", false, function(res) {
      // console.log(phone)
      // console.log(code)
      // console.log(res)
    //   if (res.code != "成功") {
    //     util.showError(res.code)
    //   } else {
    //     app.globalData.userInerInfo = res.result
    //     util.setInfoToStorage("mobile", phone)
    //     util.setInfoToStorage("verify", code)
    //     util.navigateTo("../A/index/index")
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})